package com.feidee.data.report.dao.base;

import java.lang.reflect.ParameterizedType;

import org.mybatis.spring.support.SqlSessionDaoSupport;


public abstract class BaseDao<T, K>  extends SqlSessionDaoSupport implements IBaseDao<T, K>{

    private static String SQL_ID_ADD = ".add";
    private static String SQL_ID_UPDATE = ".update";
    private static String SQL_ID_DELETE = ".delete";
    private static String SQL_ID_BY_ID = ".getById";

    private Class<T> clz;

    @SuppressWarnings("unchecked")
    private Class<T> getClz() {
        if(clz==null)
            clz = (Class<T>)((ParameterizedType)this.getClass()
                        .getGenericSuperclass()).getActualTypeArguments()[0];
        return clz;
    }


    public boolean add(T obj) {
        this.getSqlSession().insert(this.getClz().getName() + SQL_ID_ADD , obj);
        return true;
    }

    public boolean delete(K id) {
        this.getSqlSession().delete(this.getClz().getName() + SQL_ID_DELETE , id);
        return true;
    }

    public boolean update(T obj) {
        this.getSqlSession().update(this.getClz().getName() + SQL_ID_UPDATE, obj);
        return true;
    }

    @SuppressWarnings("unchecked")
    public T getById(K id) {
        return (T)this.getSqlSession().selectOne(this.getClz().getName() + SQL_ID_BY_ID , id);
    }


}
